<template>
  <div class="benefit-exchange">
    <nav-bar>
      <div class="center" slot="center">兑换红包</div>
    </nav-bar>
    <div class="main">
      <input type="text" placeholder="请输入兑换码" v-model="exchangeCode" />
      <div class="verification-code">
        <input
          type="text"
          placeholder="请输入验证码"
          v-model="verificationCode"
        />
        <div class="picture">
          <img :src="picture" alt="" class="left" />
          <span class="right" @click="handleSwitchVerificationCode">
            <i>看不清</i>
            <i>换一张</i>
          </span>
        </div>
      </div>
      <input
        type="button"
        value="兑换"
        class="exchange"
        :style="{ 'background-color': isActive ? '#4cd964' : '#cccccc' }"
        @click="handleExchange"
      />
    </div>
  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar";

import { getCaptchas } from "network/login";
import { getExchange } from "network/benefit";

export default {
  data() {
    return {
      picture: "",
      exchangeCode: "",
      verificationCode: "",
      isActive: false,
    };
  },

  watch: {
    exchangeCode(value) {
      this.isActive =
        this.verificationCode.trim().length >= 4 && Number(value) ? true : false;
    },
    verificationCode(value) {
      this.verificationCode =
        value.trim().length >= 4
          ? this.verificationCode.substring(0, 4)
          : this.verificationCode;
      this.isActive =
        value.trim().length >= 4 && Number(this.exchangeCode) ? true : false;
    },
  },
  created() {
    this.getCaptchas();
  },
  methods: {
    getCaptchas() {
      getCaptchas().then((res) => {
        this.picture = res.status === 1 ? res.code : "";
      });
    },
    getExchange(user_id, data) {
      getExchange(user_id, data).then((res) => {
        this.$toast.handleTipsText(res.message);
      });
    },
    handleSwitchVerificationCode() {
      this.getCaptchas();
    },
    handleExchange() {
      if (!this.isActive) {
        this.$toast.handleTipsText("请填写相关字段");
        return;
      } else {
        const data = {
          captcha_code: this.verificationCode,
          exchange_code: this.exchangeCode,
        };
        this.getExchange(localStorage.getItem("user_id"), data);
        this.getCaptchas()
      }
    },
  },
  components: {
    NavBar,
  },
};
</script>
<style lang="less" scoped>
.benefit-exchange {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: #f1f1f1;
  .main {
    padding: 10px;
    text-align: center;
    input {
      width: 100%;
      padding: 15px 10px;
      border-radius: 5px;
    }
    .verification-code {
      display: flex;
      justify-content: space-between;
      margin: 20px 0;
      input {
        flex: 1;
      }
      .picture {
        padding: 10px;
        margin-left: 10px;
        border-radius: 5px;
        background-color: #fff;
        .right {
          display: flex;
          flex-direction: column;
          margin-left: 5px;
          font-size: 12px;
          & i:nth-child(2) {
            margin-top: 5px;
            color: #3190e8;
          }
        }
      }
    }
    .exchange {
      padding: 10px 0;
      color: #fff;
    }
  }
}
</style>